const { http } = require("../../../utils/request");
let config = require("../../../utils/config");

// pages/mine/address/edit.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: "", //编辑的地址id
    username: "",
    telephone: "",
    provinceName: '',
    cityName: '',
    countryName: '',
    details: "",
    isDefault: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(options.id){
      this.setData({
        id: options.id
      })
      wx.setNavigationBarTitle({
        title: '地址编辑',
      })

      this.queryDetails();
    }else{
      wx.setNavigationBarTitle({
        title: '新增地址',
      })
    }
    
  },

  /** 编辑详情 */
  queryDetails(){
    let that = this;
    http.requestData("GET","MemberAddress/GetEntity",{
      Id: this.data.id
    }).then(res=>{
      if(res.Tag == 10){
        that.setData({
          username: res.Data.Name,
          telephone: res.Data.Mobile,
          provinceName: res.Data.ProvinceName,
          cityName: res.Data.CityName,
          countryName: res.Data.RegionName,
          details: res.Data.Address,
          isDefault: res.Data.IsDefault
        })
      }else{
        http.showToast(res.Message);
      }
    })
  },

  // 输入姓名
  changeName(e) {
    this.setData({
      username: e.detail.value
    })
  },
  //输入手机号
  changePhone(e) {
    this.setData({
      telephone: e.detail.value
    })
  },

  /** 选择地区 */
  changeArea(){
    let that = this;
    wx.chooseLocation({
      success(res){
        console.log(res);
        var reg = /.+?(省|市|自治区|自治州|县|区)/g;
        let myLocation = res.address.match(reg);
        if (myLocation != undefined && myLocation != null) {
          that.setData({
            provinceName: myLocation[0],
            cityName: myLocation[1],
            countryName: myLocation[2],
            details: res.address.split(myLocation[2])[1] + res.name
          })
        }
      }
    })
  },
  //输入详细地址（街道）
  changeDetails(e) {
    this.setData({
      details: e.detail.value
    })
  },

  /** 设置默认 */
  switchChange(e) {
    this.setData({
      isDefault: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /** 保存 */
  editSave(){
    if(!this.data.username){
      return http.showToast("请输入姓名")
    }
    if(!this.data.telephone){
      return http.showToast("请输入手机号")
    }
    if(!this.data.cityName){
      return http.showToast("请选择地区")
    }
    if(!this.data.details){
      return http.showToast("请输入详细地址")
    }
    http.requestData("POST","MemberAddress/Save",{
      "id": this.data.id?this.data.id:0,  //编辑，地址id
      "provinceName": this.data.provinceName,
      "cityName": this.data.cityName,
      "regionName": this.data.countryName,
      "address": this.data.details,
      "name": this.data.username,
      "mobile": this.data.telephone,
      "isDefault": this.data.isDefault
      // "memberId": "",
      // "aareaId": "",
      // "createTime": "",
      // "createName": "",
      // "modifyTime": "",
      // "modifyName": ""
    }).then(res=>{
      if(res.Tag==10){
        http.showToast("保存成功");
        setTimeout(() => {
          wx.navigateBack();
        }, 500);
      }else{
        http.showToast(res.Message)
      }
    })

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage() {}
})